package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced;

import io.netty.channel.Channel;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.NettySocksClientProxyClient;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.socket.NettySocksClientProxyClientVisitorTransferSocket;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettySocketChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocketClientProxyClientStartTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;

@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ClientHandleNettySocketProtocolHandleSocketClientProxyClientStartTypeAdvanced
        extends AbstractHandleSocketClientProxyClientStartTypeAdvanced<NettyProxyMsg> {
    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {


        NettySocketChannelContext nettySocketChannelContext = (NettySocketChannelContext) nettyChannelContext;
        Channel proxyChannel = nettySocketChannelContext.channel();
        String host = nettyProxyMsg.getTargetIpString();
        Integer port = Integer.parseInt(nettyProxyMsg.getTargetPortString());
        byte[] clientId = nettyProxyMsg.getClientId();
        byte[] visitorId = nettyProxyMsg.getVisitorId();

        ChannelAttributeKeyUtils.buildVisitorId(proxyChannel, visitorId);
        Socks5AddressType socks5AddressType = nettySocketChannelContext.getSocks5AddressType();


        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);


        NettySocksClientProxyClient nettySocksClientProxyClient = new NettySocksClientProxyClient();
        nettySocksClientProxyClient.setSsl(false);
        nettySocksClientProxyClient.setTargetIp(host);
        nettySocksClientProxyClient.setTargetPort(Integer.parseInt(String.valueOf(port)));
        nettySocksClientProxyClient.setTargetClientId(new String(clientId));
        nettySocksClientProxyClient.setSocks5AddressType(socks5AddressType);


        nettySocksClientProxyClient.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
        nettySocksClientProxyClient.setNettyClientProperties(nettyClientProperties);

        // 本地客户端创建一个访客传输通道
        NettySocksClientProxyClientVisitorTransferSocket.buildTransferServer(nettySocksClientProxyClient, proxyChannel);



    }
}
